import javax.swing.*;

// 抽象类
abstract class Shape {
    // 抽象方法
    abstract public void draw();
    abstract void callArea();

    public double getArea() {
        return area;
    }

    protected double area;
}

class Rect extends Shape {
    private double length;
    private double width;

    Rect(double length,double width) {
        this.length = length;
        this.width = width;
    }
@Override
    public void draw() {
        System.out.println("矩形：length= " + length + "width= " + width);
    }
@Override
    public void callArea() {
        area = length * width;
    System.out.println(area);
    }
}

interface USB {
    // 会被默认修饰为public static final
    double brand = 3.0;
    void openDevice();
    void closeDevice();
}

class Mouse implements USB {
    @Override
    public void openDevice() {
        System.out.println("打开鼠标");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭键盘");
    }

    public void click() {
        System.out.println("鼠标点击");
    }
}

class KeyBoard implements USB {
    @Override
    public void openDevice() {
        System.out.println("打开键盘");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭键盘");
    }

    public void inPut() {
        System.out.println("键盘输入");
    }
}

class Computer {
    public void powerOn() {
        System.out.println("打开笔记本电脑");
    }

    public void powerOff() {
        System.out.println("关闭笔记本电脑");
    }

    public void useDevice(USB usb) {
        usb.openDevice();
        //instanceof 使用它检查是否是对应的类型
        if(usb instanceof Mouse) {
            Mouse mouse = (Mouse)usb;
            mouse.click();
        } else if(usb instanceof KeyBoard) {
            KeyBoard keyBoard = (KeyBoard) usb;
            keyBoard.inPut();
        }
        usb.closeDevice();
    }
}

class A implements USB {
    // ctrl + i 快速实现接口
    @java.lang.Override
    public void openDevice() {
        System.out.println("123");
    }

    @java.lang.Override
    public void closeDevice() {
        System.out.println("456");
    }
}

public class Test {
    public static void main(String[] args) {
        Rect r = new Rect(3,6);
        r.draw();
        r.callArea();

        System.out.println();
        Mouse m = new Mouse();
        m.closeDevice();
        m.openDevice();
        m.click();

        System.out.println();
        KeyBoard k = new KeyBoard();
        k.openDevice();
        k.closeDevice();
        k.inPut();

        System.out.println();
        Computer computer = new Computer();
        computer.powerOn();
        System.out.println();
        computer.useDevice(new Mouse());
        System.out.println();
        computer.useDevice(new KeyBoard());

        System.out.println();
        A a = new A();
        a.closeDevice();
        a.openDevice();
    }
}

